/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelReport.java
 *
 * Created on Feb 15, 2012, 12:03:42 AM
 */
package GUI;

import Entity.ConnectionSQL;
import Entity.Report;
import Entity.User;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;

/**
 *
 * @author Duc
 */
public class PanelReport extends javax.swing.JPanel {
    private ResourceBundle messages;
    private User user;
    private List<User> listUser;
    /** Creates new form PanelReport */
    public PanelReport(User user,ResourceBundle messages) {
        this.messages = messages;
        this.user = user;
        initComponents();
        btnGroup.add(rbIM);
        btnGroup.add(rbSM);
        rbIM.setSelected(true);
        rbSM.setSelected(false);
        setValueCombobox();
        setLabel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        rbIM = new javax.swing.JRadioButton();
        rbSM = new javax.swing.JRadioButton();
        cbbUserName = new javax.swing.JComboBox();
        lblFullName = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaMessage = new javax.swing.JTextArea();
        btnSend = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 20));
        jLabel1.setText("Send Report");

        rbIM.setText("Inventory Manager");
        rbIM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbIMActionPerformed(evt);
            }
        });

        rbSM.setText("Sale Manager");
        rbSM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbSMActionPerformed(evt);
            }
        });

        cbbUserName.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbUserName.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbbUserNameItemStateChanged(evt);
            }
        });

        lblFullName.setText("jLabel2");

        txaMessage.setColumns(20);
        txaMessage.setRows(5);
        jScrollPane1.setViewportView(txaMessage);

        btnSend.setText("Send");
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });

        jLabel2.setText("Full Name: ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(57, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 343, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(rbIM)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(21, 21, 21)
                                        .addComponent(cbbUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(28, 28, 28)
                                        .addComponent(rbSM))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 36, Short.MAX_VALUE)
                                        .addComponent(jLabel2)
                                        .addGap(18, 18, 18)
                                        .addComponent(lblFullName, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addContainerGap(41, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(179, 179, 179))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnSend)
                        .addGap(193, 193, 193))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbIM)
                    .addComponent(rbSM))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbbUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblFullName)
                    .addComponent(jLabel2))
                .addGap(30, 30, 30)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnSend)
                .addContainerGap(19, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cbbUserNameItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbbUserNameItemStateChanged
        setLabel();
    }//GEN-LAST:event_cbbUserNameItemStateChanged

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
        send();
    }//GEN-LAST:event_btnSendActionPerformed

    private void rbIMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbIMActionPerformed
        setValueCombobox();
    }//GEN-LAST:event_rbIMActionPerformed

    private void rbSMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbSMActionPerformed
        setValueCombobox();
    }//GEN-LAST:event_rbSMActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup btnGroup;
    private javax.swing.JButton btnSend;
    private javax.swing.JComboBox cbbUserName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblFullName;
    private javax.swing.JRadioButton rbIM;
    private javax.swing.JRadioButton rbSM;
    private javax.swing.JTextArea txaMessage;
    // End of variables declaration//GEN-END:variables
    private void setValueCombobox(){
        cbbUserName.removeAllItems();
        int priority;
        if(rbIM.isSelected()) priority = 2;
        else priority = 3;
        ConnectionSQL cnn = new ConnectionSQL(messages);
        listUser = cnn.getUserByPriority(priority);
        for(User u : listUser )
        cbbUserName.addItem(u.getUserName());
    }

    private void setLabel() {
        if(cbbUserName.getSelectedItem()!= null){
        String userName = cbbUserName.getSelectedItem().toString();
        User userReveice = null;
        for(int i =0;i<listUser.size();i++){
            if(listUser.get(i).getUserName().equals(userName))
                userReveice = listUser.get(i);
        }
        lblFullName.setText(userReveice.getFirstName()+ " " + userReveice.getLastName());
    }
    }

    private void send() {
        String userName = cbbUserName.getSelectedItem().toString();
        User userReveice = null;
        for(int i =0;i<listUser.size();i++){
            if(listUser.get(i).getUserName().equals(userName))
                userReveice = listUser.get(i);
        }
        Report report = new Report(user.getId(), userReveice.getId(), txaMessage.getText());
        int i = JOptionPane.showConfirmDialog(this, messages.getString("messagesConfirmSendReport"), messages.getString("titleConfirmSendReport"), JOptionPane.YES_NO_OPTION);
       if(i == JOptionPane.OK_OPTION){
        ConnectionSQL cnn  = new ConnectionSQL(messages);
            if(cnn.sendReport(report)){
                txaMessage.setText("");
            JOptionPane.showMessageDialog(this, messages.getString("messagesSendReportComplete"));
        }
       }
    }
}

